﻿/**  版本信息模板在安装目录下，可自行修改。
* Traffic_Manager.cs
*
* 功 能： N/A
* 类 名： Traffic_Manager
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2022/11/4 22:05:14   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
namespace AgvDemo.DAL
{
    /// <summary>
    /// 数据访问类:Traffic_Manager
    /// </summary>
    public partial class Traffic_Manager
    {
        public Traffic_Manager()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQLite.GetMaxID("id", "Traffic_Manager");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Traffic_Manager");
            strSql.Append(" where id=@id");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@id", DbType.Int32,4)
            };
            parameters[0].Value = id;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(AgvDemo.Model.Traffic_Manager model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Traffic_Manager(");
            strSql.Append("traffic_area_no,lock_card_num,except_card_for_unlock,card_direction)");
            strSql.Append(" values (");
            strSql.Append("@traffic_area_no,@lock_card_num,@except_card_for_unlock,@card_direction)");
            strSql.Append(";select LAST_INSERT_ROWID()");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@traffic_area_no", DbType.Int32,4),
                    new SQLiteParameter("@lock_card_num", DbType.Int32,4),
                    new SQLiteParameter("@except_card_for_unlock", DbType.String,2147483647),
                    new SQLiteParameter("@card_direction", DbType.Int32,4)};
            parameters[0].Value = model.traffic_area_no;
            parameters[1].Value = model.lock_card_num;
            parameters[2].Value = model.except_card_for_unlock;
            parameters[3].Value = model.card_direction;

            object obj = DbHelperSQLite.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(AgvDemo.Model.Traffic_Manager model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Traffic_Manager set ");
            strSql.Append("traffic_area_no=@traffic_area_no,");
            strSql.Append("lock_card_num=@lock_card_num,");
            strSql.Append("except_card_for_unlock=@except_card_for_unlock,");
            strSql.Append("card_direction=@card_direction");
            strSql.Append(" where id=@id");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@traffic_area_no", DbType.Int32,4),
                    new SQLiteParameter("@lock_card_num", DbType.Int32,4),
                    new SQLiteParameter("@except_card_for_unlock", DbType.String,2147483647),
                    new SQLiteParameter("@card_direction", DbType.Int32,4),
                    new SQLiteParameter("@id", DbType.Int32,8)};
            parameters[0].Value = model.traffic_area_no;
            parameters[1].Value = model.lock_card_num;
            parameters[2].Value = model.except_card_for_unlock;
            parameters[3].Value = model.card_direction;
            parameters[4].Value = model.id;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Traffic_Manager ");
            strSql.Append(" where id=@id");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@id", DbType.Int32,4)
            };
            parameters[0].Value = id;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Traffic_Manager ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public AgvDemo.Model.Traffic_Manager GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,traffic_area_no,lock_card_num,except_card_for_unlock,card_direction from Traffic_Manager ");
            strSql.Append(" where id=@id");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@id", DbType.Int32,4)
            };
            parameters[0].Value = id;

            AgvDemo.Model.Traffic_Manager model = new AgvDemo.Model.Traffic_Manager();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public AgvDemo.Model.Traffic_Manager DataRowToModel(DataRow row)
        {
            AgvDemo.Model.Traffic_Manager model = new AgvDemo.Model.Traffic_Manager();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["traffic_area_no"] != null && row["traffic_area_no"].ToString() != "")
                {
                    model.traffic_area_no = int.Parse(row["traffic_area_no"].ToString());
                }
                if (row["lock_card_num"] != null && row["lock_card_num"].ToString() != "")
                {
                    model.lock_card_num = int.Parse(row["lock_card_num"].ToString());
                }
                if (row["except_card_for_unlock"] != null)
                {
                    model.except_card_for_unlock = row["except_card_for_unlock"].ToString();
                }
                if (row["card_direction"] != null && row["card_direction"].ToString() != "")
                {
                    model.card_direction = int.Parse(row["card_direction"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,traffic_area_no,lock_card_num,except_card_for_unlock,card_direction ");
            strSql.Append(" FROM Traffic_Manager ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Traffic_Manager ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQLite.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from Traffic_Manager T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "Traffic_Manager";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}


